package com.tjbank.cssys.service.impl;

import com.tjbank.cssys.service.util.Const;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.base.Captcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 验证码
 * 
 * Package : com.tansun.magicube.iam.service.impl
 * 
 * @author -- lijiangtao 2020年7月6日 下午5:50:08
 *
 */
@Service
public class CaptchaService {

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	public Map<String, Object> getSecurityCode(String user) {
		Captcha captcha = new ArithmeticCaptcha(130, 48);
		//String key = UUID.randomUUID().toString();
		String verCode = captcha.text();
		redisTemplate.opsForValue().set(Const.CAPTCHA_KEY + user, verCode, 1L, TimeUnit.MINUTES);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("key", user);
		map.put("image", captcha.toBase64());
		return map;
	}
}
